package com.orion.config.security;

import com.orion.entity.Result;
import com.orion.enumeration.Oauth2ExceptionCodeEnum;
import com.orion.util.ResponseUtil;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author Administrator
 * @date 2021/11/10
 */
@Component
public class CustomServerFailureHandler implements ServerAuthenticationFailureHandler{
    @Override
    public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
        System.out.println("in CustomServerFailureHandler :" + exception.getMessage());
        Result r = Oauth2ExceptionCodeEnum.UNAUTHORIZED.toResult();
        ServerHttpResponse serverHttpResponse = webFilterExchange.getExchange().getResponse();
        serverHttpResponse.getHeaders().add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
        return ResponseUtil.jsonResp(serverHttpResponse, r);
    }
}
